package com.cez.itstudio01.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cez.itstudio01.mapper.BookMapper;
import com.cez.itstudio01.mapper.UserMapper;
import com.cez.itstudio01.pojo.Book;
import com.cez.itstudio01.pojo.User;
import com.sun.org.apache.bcel.internal.generic.RET;
import kotlin.experimental.BitwiseOperationsKt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yaml.snakeyaml.events.Event;

import javax.servlet.http.HttpSession;
import javax.swing.text.html.HTML;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * author: chenez
 * date: 2022/9/14 21:26
 * description:
 */
@Controller
public class stuController {
    @Autowired
    BookMapper bookMapper;
    @Autowired
    UserMapper userMapper;
    /**
     * @Description 获取书本信息
     * @Date 2022/9/15 9:43
     * @param ID
     * @param session
     *@return java.lang.Object
     **/
    @RequestMapping("/index/stu/getBookInfo")
    @ResponseBody
    public Object getBookInfo(String ID, HttpSession session){
        String stuID = (String) session.getAttribute("stu.ID");
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> map1 = new HashMap<>();
        if(stuID==null){
            map.put("code","-1");
            map.put("message","请先登录");
            return map;
        }
        User user = userMapper.getByID(stuID);
        Book book = bookMapper.selectById(ID);
        if(book==null){
            map.put("code","0");
            map.put("message","查无此书");
            return map;
        }else {
            map1.put("ID",user.getId());
            map1.put("name",book.getName());
            map1.put("publisher",book.getPublisher());
            map1.put("book_now_num",book.getTotalNum()-book.getBorrowedNum());
            map1.put("user_now_num",user.getUserNowNum());
            map.put("code","1");
            map.put("data",map1);
            return map;
        }
    }
    /**
     * @Description 借书
     * @Date 2022/9/15 9:43
     * @param session
     * @param ID
     *@return java.lang.Object
     **/
    @RequestMapping("/index/stu/borrowBook")
    @ResponseBody
    public Object borrowBook(HttpSession session,String ID){
        String stuID = (String) session.getAttribute("stu.ID");
        HashMap<Object, Object> map = new HashMap<>();
        if(stuID==null){
            map.put("code","-1");
            map.put("message","请先登录");
            return map;
        }
        User user = userMapper.getByID(stuID);
        Book book = bookMapper.selectById(ID);
        if(book==null){
            map.put("code","0");
            map.put("message","查无此书");
            return map;
        }
        if(book.getTotalNum()-book.getBorrowedNum()==0){
            map.put("code","0");
            map.put("message","此书已被借完");
            return map;
        }
        if(user.getUserNowNum()==0){
            map.put("code","0");
            map.put("message","您的借书额度以用完");
            return map;
        }
        else {
            int sum = user.getUserNowNum();
            if(sum==1){
                user.setBook2Id(ID);
            }if(sum==2){
                user.setBook1Id(ID);
            }
            userMapper.updateUser(user.getId(),user.getBook1Id(),user.getBook2Id());
            book.setBorrowedNum(book.getBorrowedNum()+1);
            map.put("code","1");
            map.put("message","借出成功");
            return map;
        }

    }
    @RequestMapping("/index/stu/getBorrowInfo")
    @ResponseBody
    public Object getBorrowInfo(HttpSession session){
        String stuID =(String) session.getAttribute("stu.ID");
        User user = userMapper.getByID(stuID);
        HashMap<Object, Object> map = new HashMap<>();
        ArrayList<Object> list = new ArrayList<>();
        if(stuID==null){
            map.put("code","-1");
            map.put("message","请先登录");
            return map;
        }else {
            Book book1 = bookMapper.selectById(user.getBook1Id());
            Book book2 = bookMapper.selectById(user.getBook2Id());
            list.add(book1);
            list.add(book2);
            map.put("code","1");
            map.put("data",list);
            return map;
        }
    }
    @RequestMapping("/index/stu/restoreBook")
    @ResponseBody
    public Object restoreBook(HttpSession session,String ID){
        String stuID = (String) session.getAttribute("stu.ID");
        User user = userMapper.getByID(stuID);
        Book book = bookMapper.selectById(ID);
        HashMap<Object, Object> map = new HashMap<>();
        if(stuID==null){
            map.put("code","-1");
            map.put("message","请先登录");
            return map;
        }else {
            if(user.getBook2Id()==ID){
                user.setBook2Id(null);
            }else {
                user.setBook1Id(user.getBook2Id());
                user.setBook2Id(null);
            }
            userMapper.updateUser(stuID,user.getBook1Id(),user.getBook2Id());
            book.setBorrowedNum(book.getBorrowedNum()-1);
            bookMapper.updateById(book);
            map.put("code","1");
            map.put("message","还书成功");
            return map;
        }
    }

}
